package com.dcbeida.widget.scanitem;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.DrugCodeItem;
import com.dcbeida.bean.DrugCodeInfo;

import java.util.List;
import java.util.Map;

public class DrugScanView extends ScanView<DrugCodeItem> {

    public DrugScanView(Context context) {
        super(context);
    }

    public DrugScanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrugScanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void calculateCount(View headView, List<DrugCodeItem> list) {
        TextView tv_all_count = (TextView) headView.findViewById(R.id.tv_all_count);
        int all = list.size();
        tv_all_count.setText(all + "");
    }

    @Override
    protected View getNoHeadView() {
        View hasHeadView = getHasHeadView();
        View lin_header = hasHeadView.findViewById(R.id.lin_header);
        lin_header.setVisibility(View.GONE);
        return hasHeadView;
    }

    @Override
    protected View getHasHeadView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.drug_zy_with_parent_cancel_item, null);
    }

    @Override
    protected void setDataToView(final View itemView, final DrugCodeItem data) {
        TextView tv_ph = (TextView) itemView.findViewById(R.id.tv_ph);
        tv_ph.setText(data.getChinaid());

        TextView tv_yxq = (TextView) itemView.findViewById(R.id.tv_yxq);
        tv_yxq.setText(getContext().getString(R.string.yxq2, data.getExpiredate()));

        TextView tv_bh = (TextView) itemView.findViewById(R.id.tv_bh);
        tv_bh.setText(data.getDrugcode());

        TextView tv_box = (TextView) itemView.findViewById(R.id.tv_box);
        tv_box.setText("1");

        TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
        tv_name.setText(data.getName());
        ImageView iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);

        iv_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("fancl", "执行次数");
                DrugScanView.this.removeView(itemView);
                removeData(data);
            }
        });

    }

//    @Override
//    public int calculateCount(List<DrugCodeItem> list) {
//        int all = 0;
//        for (int i = 0; i < list.size(); i++) {
//            all += list.get(i).getQuantity();
//        }
//        return all;
//    }

    public boolean isContains(String code) {
        Map<String, List<DrugCodeItem>> datas = getDatas();
        for (Map.Entry<String, List<DrugCodeItem>> entry : datas.entrySet()) {
            List<DrugCodeItem> value = entry.getValue();
            for (DrugCodeItem drugCodeItem : value) {
                if (code.equalsIgnoreCase(drugCodeItem.getDrugcode())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void closeOperate() {

    }

    @Override
    public String getKey(DrugCodeItem data) {
        return data.getDrugid();
    }
}
